/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.auth;

import hu.glassy.core.model.Account;
import hu.glassy.core.service.AccountService;
import hu.glassy.web.model.AccountModel;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.request.Request;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Session
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class ApplicationSession extends AuthenticatedWebSession
{

  private static final long serialVersionUID = -7548528290208982613L;

  @SpringBean
  private AccountService accountService;

  private final Logger logger = LoggerFactory.getLogger(ApplicationSession.class);
  private AccountModel model;

  public ApplicationSession(final Request request)
  {
    super(request);

    Injector.get().inject(ApplicationSession.this);
  }

  @Override
  public boolean authenticate(final String email, final String encryptedPassword)
  {
    final Account account = accountService.findByEmailAndPassword(email, encryptedPassword);

    if (account == null)
    {
      if (logger.isInfoEnabled())
      {
        logger.info("Authentication failed with e-mail address: " + email);
      }
      return false;
    }

    model = new AccountModel(account);
    accountService.updateLastLoginDate(account);

    if (logger.isInfoEnabled())
    {
      logger.info("Successfull authentication with e-mail address: " + email);
    }
    return true;
  }

  @Override
  public Roles getRoles()
  {
    if (model == null || model.getObject() == null)
    {
      return new Roles();
    }

    final Roles roles = new Roles(Roles.USER);
    if (model.getObject().isAdmin())
    {
      roles.add(Roles.ADMIN);
    }
    return roles;
  }

  @Override
  public void invalidate()
  {
    super.invalidate();

    if (logger.isInfoEnabled())
    {
      logger.info("Session invalidated");
    }
  }

  // --------------------------------------------------
  // ~ Public methods
  // --------------------------------------------------
  /**
   * A bejelentkezett felhasználóhoz tartozó accountot adja vissza
   *
   * @return
   */
  public AccountModel getAccount()
  {
    return model;
  }

  // --------------------------------------------------
  // ~ Static methods
  // --------------------------------------------------
  public static ApplicationSession get()
  {
    return (ApplicationSession) AuthenticatedWebSession.get();
  }
}
